################################################################################################
###Replication Code for "Because He's Gay": How Race, Gender, and Sexuality Shape Perceptions of Judicial Fairness
###Journal of Politics
###Authors: Ana Bracic, Mackenzie Israel-Trummel, Tyler Johnson, and Kathleen Tipler
################################


################################
## Instructions:
## This code file cleans the survey data and then sets up the data frame for analysis. 
## Run this code file first.
################################


#########################
## clear workspace
## load R libraries
## set working directory
#########################

rm(list=ls())
library(rlang)
library(car)
library(ggplot2)
library(reshape)
library(survey)
library(mice)
library(miceadds)
library(sandwich)



setwd()

#########################
## Import raw csv file
## The file has two lines of string headers
#########################



df.raw <- read.csv(file = "conjoint_judgesdata.csv", skip = 0, head = T) #load data file which includes both variable names and the second row has the full text of questions

varnames <- names(df.raw) #extract names of variables

df.raw2 <- read.csv(file = "conjoint_judgesdata.csv", skip = 2, head = F) #load data file without variable names and question text
names(df.raw2) <- varnames #add variable names



#subset to respondents who completed all 5 conjoint pairings

df <- subset(df.raw2,df.raw2$Q117_2 != "")  
names(df)


table(df$Q117_2) #N= 4042 RESPONDENTS COMPLETED THE 5 PAIRINGS

#####################################################################################################
## CLEAN DEPENDENT VARIABLES
#####################################################################################################

##Recode impartiality measures to range from 0 to 6

table(df$Q81_1)
df$Q81_1 <- df$Q81_1-1

table(df$Q81_2)
df$Q81_2 <- df$Q81_2-1

table(df$Q88_1)
df$Q88_1 <- df$Q88_1-1

table(df$Q88_2)
df$Q88_2 <- df$Q88_2-1

table(df$Q95_1)
df$Q95_1 <- df$Q95_1-1

table(df$Q95_2)
df$Q95_2 <- df$Q95_2-1

table(df$Q109_1)
df$Q109_1 <- df$Q109_1-1

table(df$Q109_2)
df$Q109_2 <- df$Q109_2-1

table(df$Q116_1)
df$Q116_1 <- df$Q116_1-1

table(df$Q116_2)
df$Q116_2 <- df$Q116_2-1


##Recode judges' perceived ideology variables to range from 0 to 6

table(df$Q5_1)
df$Q5_1num[df$Q5_1 =="Very liberal"] <- 0 
df$Q5_1num[df$Q5_1 =="Liberal"] <- 1
df$Q5_1num[df$Q5_1 =="Slightly liberal"] <- 2
df$Q5_1num[df$Q5_1 =="Moderate"] <- 3
df$Q5_1num[df$Q5_1 =="Slightly conservative"] <- 4 
df$Q5_1num[df$Q5_1 =="Conservative"] <- 5 
df$Q5_1num[df$Q5_1 =="Very conservative"] <- 6
table(df$Q5_1, df$Q5_1num)

table(df$Q5_2)
df$Q5_2num[df$Q5_2 =="Very liberal"] <- 0 
df$Q5_2num[df$Q5_2 =="Liberal"] <- 1
df$Q5_2num[df$Q5_2 =="Slightly liberal"] <- 2
df$Q5_2num[df$Q5_2 =="Moderate"] <- 3
df$Q5_2num[df$Q5_2 =="Slightly conservative"] <- 4 
df$Q5_2num[df$Q5_2 =="Conservative"] <- 5 
df$Q5_2num[df$Q5_2 =="Very conservative"] <- 6
table(df$Q5_2, df$Q5_2num)


table(df$Q87_1)
df$Q87_1num[df$Q87_1 =="Very liberal"] <- 0 
df$Q87_1num[df$Q87_1 =="Liberal"] <- 1
df$Q87_1num[df$Q87_1 =="Slightly liberal"] <- 2
df$Q87_1num[df$Q87_1 =="Moderate"] <- 3
df$Q87_1num[df$Q87_1 =="Slightly conservative"] <- 4 
df$Q87_1num[df$Q87_1 =="Conservative"] <- 5 
df$Q87_1num[df$Q87_1 =="Very conservative"] <- 6
table(df$Q87_1, df$Q87_1num)

table(df$Q87_2)
df$Q87_2num[df$Q87_2 =="Very liberal"] <- 0 
df$Q87_2num[df$Q87_2 =="Liberal"] <- 1
df$Q87_2num[df$Q87_2 =="Slightly liberal"] <- 2
df$Q87_2num[df$Q87_2 =="Moderate"] <- 3
df$Q87_2num[df$Q87_2 =="Slightly conservative"] <- 4 
df$Q87_2num[df$Q87_2 =="Conservative"] <- 5 
df$Q87_2num[df$Q87_2 =="Very conservative"] <- 6
table(df$Q87_2, df$Q87_2num)

table(df$Q94_1)
df$Q94_1num[df$Q94_1 =="Very liberal"] <- 0 
df$Q94_1num[df$Q94_1 =="Liberal"] <- 1
df$Q94_1num[df$Q94_1 =="Slightly liberal"] <- 2
df$Q94_1num[df$Q94_1 =="Moderate"] <- 3
df$Q94_1num[df$Q94_1 =="Slightly conservative"] <- 4 
df$Q94_1num[df$Q94_1 =="Conservative"] <- 5 
df$Q94_1num[df$Q94_1 =="Very conservative"] <- 6
table(df$Q94_1, df$Q94_1num)

table(df$Q94_2)
df$Q94_2num[df$Q94_2 =="Very liberal"] <- 0 
df$Q94_2num[df$Q94_2 =="Liberal"] <- 1
df$Q94_2num[df$Q94_2 =="Slightly liberal"] <- 2
df$Q94_2num[df$Q94_2 =="Moderate"] <- 3
df$Q94_2num[df$Q94_2 =="Slightly conservative"] <- 4 
df$Q94_2num[df$Q94_2 =="Conservative"] <- 5 
df$Q94_2num[df$Q94_2 =="Very conservative"] <- 6
table(df$Q94_2, df$Q94_2num)

table(df$Q108_1)
df$Q108_1num[df$Q108_1 =="Very liberal"] <- 0 
df$Q108_1num[df$Q108_1 =="Liberal"] <- 1
df$Q108_1num[df$Q108_1 =="Slightly liberal"] <- 2
df$Q108_1num[df$Q108_1 =="Moderate"] <- 3
df$Q108_1num[df$Q108_1 =="Slightly conservative"] <- 4 
df$Q108_1num[df$Q108_1 =="Conservative"] <- 5 
df$Q108_1num[df$Q108_1 =="Very conservative"] <- 6
table(df$Q108_1, df$Q108_1num)

table(df$Q108_2)
df$Q108_2num[df$Q108_2 =="Very liberal"] <- 0 
df$Q108_2num[df$Q108_2 =="Liberal"] <- 1
df$Q108_2num[df$Q108_2 =="Slightly liberal"] <- 2
df$Q108_2num[df$Q108_2 =="Moderate"] <- 3
df$Q108_2num[df$Q108_2 =="Slightly conservative"] <- 4 
df$Q108_2num[df$Q108_2 =="Conservative"] <- 5 
df$Q108_2num[df$Q108_2 =="Very conservative"] <- 6
table(df$Q108_2, df$Q108_2num)

table(df$Q115_1)
df$Q115_1num[df$Q115_1 =="Very liberal"] <- 0 
df$Q115_1num[df$Q115_1 =="Liberal"] <- 1
df$Q115_1num[df$Q115_1 =="Slightly liberal"] <- 2
df$Q115_1num[df$Q115_1 =="Moderate"] <- 3
df$Q115_1num[df$Q115_1 =="Slightly conservative"] <- 4 
df$Q115_1num[df$Q115_1 =="Conservative"] <- 5 
df$Q115_1num[df$Q115_1 =="Very conservative"] <- 6
table(df$Q115_1, df$Q115_1num)


table(df$Q115_2)
df$Q115_2num[df$Q115_2 =="Very liberal"] <- 0 
df$Q115_2num[df$Q115_2 =="Liberal"] <- 1
df$Q115_2num[df$Q115_2 =="Slightly liberal"] <- 2
df$Q115_2num[df$Q115_2 =="Moderate"] <- 3
df$Q115_2num[df$Q115_2 =="Slightly conservative"] <- 4 
df$Q115_2num[df$Q115_2 =="Conservative"] <- 5 
df$Q115_2num[df$Q115_2 =="Very conservative"] <- 6
table(df$Q115_2, df$Q115_2num)


##Recode trust in USSC if judge were nominated variable to range from 0 to 3

table(df$Q82_1)
df$Q82_1num[df$Q82_1 == "Not at all"] <- 0
df$Q82_1num[df$Q82_1 == "Not too much"] <- 1
df$Q82_1num[df$Q82_1 == "A fair amount"] <- 2
df$Q82_1num[df$Q82_1 == "A great deal"] <- 3
table(df$Q82_1, df$Q82_1num)

table(df$Q82_2)
df$Q82_2num[df$Q82_2 == "Not at all"] <- 0
df$Q82_2num[df$Q82_2 == "Not too much"] <- 1
df$Q82_2num[df$Q82_2 == "A fair amount"] <- 2
df$Q82_2num[df$Q82_2 == "A great deal"] <- 3
table(df$Q82_2, df$Q82_2num)

table(df$Q89_1)
df$Q89_1num[df$Q89_1 == "Not at all"] <- 0
df$Q89_1num[df$Q89_1 == "Not too much"] <- 1
df$Q89_1num[df$Q89_1 == "A fair amount"] <- 2
df$Q89_1num[df$Q89_1 == "A great deal"] <- 3
table(df$Q89_1, df$Q89_1num)

table(df$Q89_2)
df$Q89_2num[df$Q89_2 == "Not at all"] <- 0
df$Q89_2num[df$Q89_2 == "Not too much"] <- 1
df$Q89_2num[df$Q89_2 == "A fair amount"] <- 2
df$Q89_2num[df$Q89_2 == "A great deal"] <- 3
table(df$Q89_2, df$Q89_2num)

table(df$Q96_1)
df$Q96_1num[df$Q96_1 == "Not at all"] <- 0
df$Q96_1num[df$Q96_1 == "Not too much"] <- 1
df$Q96_1num[df$Q96_1 == "A fair amount"] <- 2
df$Q96_1num[df$Q96_1 == "A great deal"] <- 3
table(df$Q96_1, df$Q96_1num)

table(df$Q96_2)
df$Q96_2num[df$Q96_2 == "Not at all"] <- 0
df$Q96_2num[df$Q96_2 == "Not too much"] <- 1
df$Q96_2num[df$Q96_2 == "A fair amount"] <- 2
df$Q96_2num[df$Q96_2 == "A great deal"] <- 3
table(df$Q96_2, df$Q96_2num)

table(df$Q110_1)
df$Q110_1num[df$Q110_1 == "Not at all"] <- 0
df$Q110_1num[df$Q110_1 == "Not too much"] <- 1
df$Q110_1num[df$Q110_1 == "A fair amount"] <- 2
df$Q110_1num[df$Q110_1 == "A great deal"] <- 3
table(df$Q110_1, df$Q110_1num)

table(df$Q110_2)
df$Q110_2num[df$Q110_2 == "Not at all"] <- 0
df$Q110_2num[df$Q110_2 == "Not too much"] <- 1
df$Q110_2num[df$Q110_2 == "A fair amount"] <- 2
df$Q110_2num[df$Q110_2 == "A great deal"] <- 3
table(df$Q110_2, df$Q110_2num)

table(df$Q117_1)
df$Q117_1num[df$Q117_1 == "Not at all"] <- 0
df$Q117_1num[df$Q117_1 == "Not too much"] <- 1
df$Q117_1num[df$Q117_1 == "A fair amount"] <- 2
df$Q117_1num[df$Q117_1 == "A great deal"] <- 3
table(df$Q117_1, df$Q117_1num)

table(df$Q117_2)
df$Q117_2num[df$Q117_2 == "Not at all"] <- 0
df$Q117_2num[df$Q117_2 == "Not too much"] <- 1
df$Q117_2num[df$Q117_2 == "A fair amount"] <- 2
df$Q117_2num[df$Q117_2 == "A great deal"] <- 3
table(df$Q117_2, df$Q117_2num)


#####################################################################################################
## CLEAN RESPONDENT DEMOGRAPHIC VARIABLES
#####################################################################################################

##partisanship 

table(df$Q156_1) 
df$Rpid <- df$Q156_1
summary(df$Rpid) ##41 percent dems, 38 percent reps. mean: 3.86, median 4

table(df$Q125)
table(df$Q152)
df$Rwoman[df$Q125 == "Woman"] <- 1
df$Rwoman[df$Q125 == "Man"] <- 0
df$Rwoman[df$Q152 == "Woman"] <- 1
df$Rwoman[df$Q152 == "Man"] <- 0
table(df$Rwoman)

table(df$Q120)
df$RLGB[df$Q120== "No"] <- 0
df$RLGB[df$Q120== "Unsure"] <- 1
df$RLGB[df$Q120== "Yes"] <- 1

df$RLGB2[df$Q120== "No"] <- 0
df$RLGB2[df$Q120== "Unsure"] <- 0
df$RLGB2[df$Q120== "Yes"] <- 1
table(df$RLGB)
table(df$RLGB2)

table(df$Q119)
df$Rtrans[df$Q119 == "Yes"] <- 1
df$Rtrans[df$Q119 == "No"] <- 0
df$Rtrans[df$Q119 == "Unsure"] <- 0
table(df$Rtrans)

table(df$Q122)
df$Rincome[df$Q122 == "Less than $20,000"] <- 0
df$Rincome[df$Q122 == "$20,000 to $29,999"] <- 1
df$Rincome[df$Q122 == "$30,000 to $39,999"] <- 2
df$Rincome[df$Q122 == "$40,000 to $49,999"] <- 3
df$Rincome[df$Q122 == "$50,000 to $59,999"] <- 4
df$Rincome[df$Q122 == "$60,000 to $69,999"] <- 5
df$Rincome[df$Q122 == "$70,000 to $79,999"] <- 6
df$Rincome[df$Q122 == "$80,000 to $89,999"] <- 7
df$Rincome[df$Q122 == "$90,000 to $99,999"] <- 8
df$Rincome[df$Q122 == "$100,000 to $149,999"] <- 9
df$Rincome[df$Q122 == "$150,000 to $199,999"] <- 10
df$Rincome[df$Q122 == "$200,000 or more"] <- 11
table(df$Q122, df$Rincome)

table(df$Q121 )
df$Rrelig[df$Q121 == "Never"] <- 0 
df$Rrelig[df$Q121 == "Hardly ever"] <- 1 
df$Rrelig[df$Q121 == "Only a few times during the year"] <- 2 
df$Rrelig[df$Q121 == "A few times a month"] <- 3 
df$Rrelig[df$Q121 == "Almost every week"] <- 4
df$Rrelig[df$Q121 == "At least every week"] <- 5 
table(df$Q121, df$Rrelig)


table(df$Q161)
df$Rlibcon[df$Q161 == "Very liberal"] <- 0
df$Rlibcon[df$Q161 == "Liberal"] <- 1
df$Rlibcon[df$Q161 == "Slightly liberal"] <- 2
df$Rlibcon[df$Q161 == "Moderate"] <- 3
df$Rlibcon[df$Q161 == "Slightly conservative"] <- 4
df$Rlibcon[df$Q161 == "Conservative"] <- 5
df$Rlibcon[df$Q161 == "Very conservative"] <- 6
table(df$Rlibcon, df$Q161)
table(df$Rlibcon) ##35 percent liberals, 31 percent conservatives, mean = 2.9, median 3

#race
table(df$Q154_1) #native
table(df$Q154_2) # asian
table(df$Q154_3) #black
table(df$Q154_4) #white
table(df$Q154_5) #other
table(df$Q154_6) #latino

table(df$Q154_1, df$Q154_2) 
table(df$Q154_1, df$Q154_3) 
table(df$Q154_1, df$Q154_4) 
table(df$Q154_1, df$Q154_5) #0 both
table(df$Q154_1, df$Q154_6) 

table(df$Q154_2, df$Q154_3) 
table(df$Q154_2, df$Q154_4) 
table(df$Q154_2, df$Q154_5) 
table(df$Q154_2, df$Q154_6) 

table(df$Q154_3, df$Q154_4) 
table(df$Q154_3, df$Q154_5) 
table(df$Q154_3, df$Q154_6) 

table(df$Q154_4, df$Q154_5) 
table(df$Q154_4, df$Q154_6) 

table(df$Q154_5, df$Q154_6) 

df$Rrace[df$Q154_1 == "American Indian/Native American"] <- "Native"
df$Rrace[df$Q154_2 == "Asian American/Pacific Islander"] <- "Asian"
df$Rrace[df$Q154_3 == "Black/African American"] <- "Black"
df$Rrace[df$Q154_4 == "White"] <- "White"
df$Rrace[df$Q154_5 == "Other"] <- "Other"
df$Rrace[df$Q154_6 == "Latino/Hispanic"] <- "Latino"
#code multiracial Rs
df$Rrace[df$Q154_1 == "American Indian/Native American" & df$Q154_2 == "Asian American/Pacific Islander"] <- "Multiracial"
df$Rrace[df$Q154_1 == "American Indian/Native American" & df$Q154_3 == "Black/African American"] <- "Multiracial"
df$Rrace[df$Q154_1 == "American Indian/Native American" & df$Q154_4 == "White"] <- "Multiracial"
df$Rrace[df$Q154_1 == "American Indian/Native American" & df$Q154_5 == "Other"] <- "Multiracial"
df$Rrace[df$Q154_1 == "American Indian/Native American" & df$Q154_6 == "Latino/Hispanic"] <- "Multiracial"

df$Rrace[df$Q154_2 == "Asian American/Pacific Islander" & df$Q154_3 == "Black/African American"] <- "Multiracial"
df$Rrace[df$Q154_2 == "Asian American/Pacific Islander" & df$Q154_4 == "White"] <- "Multiracial"
df$Rrace[df$Q154_2 == "Asian American/Pacific Islander" & df$Q154_5 == "Other"] <- "Multiracial"
df$Rrace[df$Q154_2 == "Asian American/Pacific Islander" & df$Q154_6 == "Latino/Hispanic"] <- "Multiracial"

df$Rrace[df$Q154_3 == "Black/African American" & df$Q154_4 == "White"] <- "Multiracial"
df$Rrace[df$Q154_3 == "Black/African American" & df$Q154_5 == "Other"] <- "Multiracial"
df$Rrace[df$Q154_3 == "Black/African American" & df$Q154_6 == "Latino/Hispanic"] <- "Multiracial"

df$Rrace[df$Q154_4 == "White" & df$Q154_5 == "Other"] <- "Multiracial"
df$Rrace[df$Q154_4 == "White" & df$Q154_6 == "Latino/Hispanic"] <- "Multiracial"

df$Rrace[df$Q154_5 == "Other" & df$Q154_6 == "Latino/Hispanic"] <- "Multiracial"


table(df$Rrace)

#education
table(df$Q151)
df$Redu[df$Q151 == "Did not finish high school"] <- 0
df$Redu[df$Q151 == "High school diploma or equivalent, no further schooling"] <- 1
df$Redu[df$Q151 == "Technical or vocational school after high school"] <- 2
df$Redu[df$Q151 == "Some college, no degree"] <- 2
df$Redu[df$Q151 == "Associate's or two-year college degree"] <- 3
df$Redu[df$Q151 == "Four-year college degree"] <- 4
df$Redu[df$Q151 == "Graduate or professional school after college, no degree"] <- 5
df$Redu[df$Q151 == "Graduate or professional degree"] <- 6
table(df$Q151, df$Redu)

#age 
table(df$Q150)
df$Rage[df$Q150 == "18-29"] <- 0
df$Rage[df$Q150 == "30-44"] <- 1
df$Rage[df$Q150 == "45-64"] <- 2
df$Rage[df$Q150 == "65 or older"] <- 3

table(df$Q150, df$Rage)

#knowledge: Q118 6 is correct, Q155 # of justices, 
table(df$Q118)
df$Rknow1[df$Q118 == "2 years"] <- 0
df$Rknow1[df$Q118 == "4 years"] <- 0
df$Rknow1[df$Q118 == "6 years"] <- 0
df$Rknow1[df$Q118 == "10 years"] <- 0
df$Rknow1[df$Q118 == "20 years"] <- 0
df$Rknow1[df$Q118 == "Lifetime"] <- 1
table(df$Q118, df$Rknow1)

table(df$Q155)
df$Rknow2 <- 0
df$Rknow2[df$Q155 == 9] <- 1
table(df$Q155, df$Rknow2)
table(df$Rknow2)

table(df$Rknow1, df$Rknow2)

df$Rknow <- df$Rknow1 + df$Rknow2

table(df$Rknow)



#####################################################################################################
## CODE SHARED ID (JUDGE AND RESPONDENT) VARIABLES FOR APPENDIX MODELS
#####################################################################################################


#share gender

#pair 1
table(df$pair1_2, df$Rwoman)
df$judge1_sharegender[df$pair1_2 == "Man" & df$Rwoman == 0] <- 1
df$judge1_sharegender[df$pair1_2 == "Man" & df$Rwoman == 1] <- 0
df$judge1_sharegender[df$pair1_2 == "Woman" & df$Rwoman == 0] <- 0
df$judge1_sharegender[df$pair1_2 == "Woman" & df$Rwoman == 1] <- 1
table(df$judge1_sharegender)

table(df$pair1_9, df$Rwoman)
df$judge2_sharegender[df$pair1_9 == "Man" & df$Rwoman == 0] <- 1
df$judge2_sharegender[df$pair1_9 == "Man" & df$Rwoman == 1] <- 0
df$judge2_sharegender[df$pair1_9 == "Woman" & df$Rwoman == 0] <- 0
df$judge2_sharegender[df$pair1_9 == "Woman" & df$Rwoman == 1] <- 1
table(df$judge2_sharegender)

#pair 2
table(df$pair2_2, df$Rwoman)
df$judge3_sharegender[df$pair2_2 == "Man" & df$Rwoman == 0] <- 1
df$judge3_sharegender[df$pair2_2 == "Man" & df$Rwoman == 1] <- 0
df$judge3_sharegender[df$pair2_2 == "Woman" & df$Rwoman == 0] <- 0
df$judge3_sharegender[df$pair2_2 == "Woman" & df$Rwoman == 1] <- 1
table(df$judge3_sharegender)

table(df$pair2_9, df$Rwoman)
df$judge4_sharegender[df$pair2_9 == "Man" & df$Rwoman == 0] <- 1
df$judge4_sharegender[df$pair2_9 == "Man" & df$Rwoman == 1] <- 0
df$judge4_sharegender[df$pair2_9 == "Woman" & df$Rwoman == 0] <- 0
df$judge4_sharegender[df$pair2_9 == "Woman" & df$Rwoman == 1] <- 1
table(df$judge4_sharegender)


#pair 3
table(df$pair3_2, df$Rwoman)
df$judge5_sharegender[df$pair3_2 == "Man" & df$Rwoman == 0] <- 1
df$judge5_sharegender[df$pair3_2 == "Man" & df$Rwoman == 1] <- 0
df$judge5_sharegender[df$pair3_2 == "Woman" & df$Rwoman == 0] <- 0
df$judge5_sharegender[df$pair3_2 == "Woman" & df$Rwoman == 1] <- 1
table(df$judge5_sharegender)

table(df$pair3_9, df$Rwoman)
df$judge6_sharegender[df$pair3_9 == "Man" & df$Rwoman == 0] <- 1
df$judge6_sharegender[df$pair3_9 == "Man" & df$Rwoman == 1] <- 0
df$judge6_sharegender[df$pair3_9 == "Woman" & df$Rwoman == 0] <- 0
df$judge6_sharegender[df$pair3_9 == "Woman" & df$Rwoman == 1] <- 1
table(df$judge6_sharegender)


#pair 4
table(df$pair4_2, df$Rwoman)
df$judge7_sharegender[df$pair4_2 == "Man" & df$Rwoman == 0] <- 1
df$judge7_sharegender[df$pair4_2 == "Man" & df$Rwoman == 1] <- 0
df$judge7_sharegender[df$pair4_2 == "Woman" & df$Rwoman == 0] <- 0
df$judge7_sharegender[df$pair4_2 == "Woman" & df$Rwoman == 1] <- 1
table(df$judge7_sharegender)

table(df$pair4_9, df$Rwoman)
df$judge8_sharegender[df$pair4_9 == "Man" & df$Rwoman == 0] <- 1
df$judge8_sharegender[df$pair4_9 == "Man" & df$Rwoman == 1] <- 0
df$judge8_sharegender[df$pair4_9 == "Woman" & df$Rwoman == 0] <- 0
df$judge8_sharegender[df$pair4_9 == "Woman" & df$Rwoman == 1] <- 1
table(df$judge8_sharegender)

#pair 5
table(df$pair5_2, df$Rwoman)
df$judge9_sharegender[df$pair5_2 == "Man" & df$Rwoman == 0] <- 1
df$judge9_sharegender[df$pair5_2 == "Man" & df$Rwoman == 1] <- 0
df$judge9_sharegender[df$pair5_2 == "Woman" & df$Rwoman == 0] <- 0
df$judge9_sharegender[df$pair5_2 == "Woman" & df$Rwoman == 1] <- 1
table(df$judge9_sharegender)

table(df$pair5_9, df$Rwoman)
df$judge10_sharegender[df$pair5_9 == "Man" & df$Rwoman == 0] <- 1
df$judge10_sharegender[df$pair5_9 == "Man" & df$Rwoman == 1] <- 0
df$judge10_sharegender[df$pair5_9 == "Woman" & df$Rwoman == 0] <- 0
df$judge10_sharegender[df$pair5_9 == "Woman" & df$Rwoman == 1] <- 1
table(df$judge10_sharegender)

##code sharedsexuality/ish (note that this is a match for straight judges/respondents but counting shared if respondent coded as 1 on RLGB2, which might not be gay)
#pair 1

table(df$pair1_3, df$RLGB2)
df$judge1_sharesexid[df$pair1_3 == "Straight" & df$RLGB2 == 0] <- 1
df$judge1_sharesexid[df$pair1_3 == "Straight" & df$RLGB2 == 1] <- 0
df$judge1_sharesexid[df$pair1_3 == "Gay" & df$RLGB2 == 1] <- 1
df$judge1_sharesexid[df$pair1_3 == "Gay" & df$RLGB2 == 0] <- 0
table(df$judge1_sharesexid)

table(df$pair1_10, df$RLGB2)
df$judge2_sharesexid[df$pair1_10 == "Straight" & df$RLGB2 == 0] <- 1
df$judge2_sharesexid[df$pair1_10 == "Straight" & df$RLGB2 == 1] <- 0
df$judge2_sharesexid[df$pair1_10 == "Gay" & df$RLGB2 == 1] <- 1
df$judge2_sharesexid[df$pair1_10 == "Gay" & df$RLGB2 == 0] <- 0
table(df$judge2_sharesexid)


#pair 2

table(df$pair2_3, df$RLGB2)
df$judge3_sharesexid[df$pair2_3 == "Straight" & df$RLGB2 == 0] <- 1
df$judge3_sharesexid[df$pair2_3 == "Straight" & df$RLGB2 == 1] <- 0
df$judge3_sharesexid[df$pair2_3 == "Gay" & df$RLGB2 == 1] <- 1
df$judge3_sharesexid[df$pair2_3 == "Gay" & df$RLGB2 == 0] <- 0
table(df$judge3_sharesexid)

table(df$pair2_10, df$RLGB2)
df$judge4_sharesexid[df$pair2_10 == "Straight" & df$RLGB2 == 0] <- 1
df$judge4_sharesexid[df$pair2_10 == "Straight" & df$RLGB2 == 1] <- 0
df$judge4_sharesexid[df$pair2_10 == "Gay" & df$RLGB2 == 1] <- 1
df$judge4_sharesexid[df$pair2_10 == "Gay" & df$RLGB2 == 0] <- 0
table(df$judge4_sharesexid)


#pair 3

table(df$pair3_3, df$RLGB2)
df$judge5_sharesexid[df$pair3_3 == "Straight" & df$RLGB2 == 0] <- 1
df$judge5_sharesexid[df$pair3_3 == "Straight" & df$RLGB2 == 1] <- 0
df$judge5_sharesexid[df$pair3_3 == "Gay" & df$RLGB2 == 1] <- 1
df$judge5_sharesexid[df$pair3_3 == "Gay" & df$RLGB2 == 0] <- 0
table(df$judge5_sharesexid)

table(df$pair3_10, df$RLGB2)
df$judge6_sharesexid[df$pair3_10 == "Straight" & df$RLGB2 == 0] <- 1
df$judge6_sharesexid[df$pair3_10 == "Straight" & df$RLGB2 == 1] <- 0
df$judge6_sharesexid[df$pair3_10 == "Gay" & df$RLGB2 == 1] <- 1
df$judge6_sharesexid[df$pair3_10 == "Gay" & df$RLGB2 == 0] <- 0
table(df$judge6_sharesexid)


#pair 4

table(df$pair4_3, df$RLGB2)
df$judge7_sharesexid[df$pair4_3 == "Straight" & df$RLGB2 == 0] <- 1
df$judge7_sharesexid[df$pair4_3 == "Straight" & df$RLGB2 == 1] <- 0
df$judge7_sharesexid[df$pair4_3 == "Gay" & df$RLGB2 == 1] <- 1
df$judge7_sharesexid[df$pair4_3 == "Gay" & df$RLGB2 == 0] <- 0
table(df$judge7_sharesexid)

table(df$pair4_10, df$RLGB2)
df$judge8_sharesexid[df$pair4_10 == "Straight" & df$RLGB2 == 0] <- 1
df$judge8_sharesexid[df$pair4_10 == "Straight" & df$RLGB2 == 1] <- 0
df$judge8_sharesexid[df$pair4_10 == "Gay" & df$RLGB2 == 1] <- 1
df$judge8_sharesexid[df$pair4_10 == "Gay" & df$RLGB2 == 0] <- 0
table(df$judge8_sharesexid)

#pair 5

table(df$pair5_3, df$RLGB2)
df$judge9_sharesexid[df$pair5_3 == "Straight" & df$RLGB2 == 0] <- 1
df$judge9_sharesexid[df$pair5_3 == "Straight" & df$RLGB2 == 1] <- 0
df$judge9_sharesexid[df$pair5_3 == "Gay" & df$RLGB2 == 1] <- 1
df$judge9_sharesexid[df$pair5_3 == "Gay" & df$RLGB2 == 0] <- 0
table(df$judge9_sharesexid)

table(df$pair5_10, df$RLGB2)
df$judge10_sharesexid[df$pair5_10 == "Straight" & df$RLGB2 == 0] <- 1
df$judge10_sharesexid[df$pair5_10 == "Straight" & df$RLGB2 == 1] <- 0
df$judge10_sharesexid[df$pair5_10 == "Gay" & df$RLGB2 == 1] <- 1
df$judge10_sharesexid[df$pair5_10 == "Gay" & df$RLGB2 == 0] <- 0
table(df$judge10_sharesexid)

#share party: keeps the independents and codes them as not matching party for both Republicans and Democrats, but note that independents won't be analyzed in the appendix model
#pair 1

table(df$pair1_4, df$Rpid)
df$judge1_shareparty[df$pair1_4 == "Democrat" & df$Rpid < 4] <- 1
df$judge1_shareparty[df$pair1_4 == "Democrat" & df$Rpid > 3] <- 0
df$judge1_shareparty[df$pair1_4 == "Republican" & df$Rpid > 4] <- 1
df$judge1_shareparty[df$pair1_4 == "Republican" & df$Rpid < 5] <- 0
table(df$judge1_shareparty)

table(df$pair1_11, df$Rpid)
df$judge2_shareparty[df$pair1_11 == "Democrat" & df$Rpid < 4] <- 1
df$judge2_shareparty[df$pair1_11 == "Democrat" & df$Rpid > 3] <- 0
df$judge2_shareparty[df$pair1_11 == "Republican" & df$Rpid > 4] <- 1
df$judge2_shareparty[df$pair1_11 == "Republican" & df$Rpid < 5] <- 0
table(df$judge2_shareparty)


#pair 2

table(df$pair2_4, df$Rpid)
df$judge3_shareparty[df$pair2_4 == "Democrat" & df$Rpid < 4] <- 1
df$judge3_shareparty[df$pair2_4 == "Democrat" & df$Rpid > 3] <- 0
df$judge3_shareparty[df$pair2_4 == "Republican" & df$Rpid > 4] <- 1
df$judge3_shareparty[df$pair2_4 == "Republican" & df$Rpid < 5] <- 0
table(df$judge3_shareparty)

table(df$pair2_11, df$Rpid)
df$judge4_shareparty[df$pair2_11 == "Democrat" & df$Rpid < 4] <- 1
df$judge4_shareparty[df$pair2_11 == "Democrat" & df$Rpid > 3] <- 0
df$judge4_shareparty[df$pair2_11 == "Republican" & df$Rpid > 4] <- 1
df$judge4_shareparty[df$pair2_11 == "Republican" & df$Rpid < 5] <- 0
table(df$judge4_shareparty)

#pair 3

table(df$pair3_4, df$Rpid)
df$judge5_shareparty[df$pair3_4 == "Democrat" & df$Rpid < 4] <- 1
df$judge5_shareparty[df$pair3_4 == "Democrat" & df$Rpid > 3] <- 0
df$judge5_shareparty[df$pair3_4 == "Republican" & df$Rpid > 4] <- 1
df$judge5_shareparty[df$pair3_4 == "Republican" & df$Rpid < 5] <- 0
table(df$judge5_shareparty)

table(df$pair3_11, df$Rpid)
df$judge6_shareparty[df$pair3_11 == "Democrat" & df$Rpid < 4] <- 1
df$judge6_shareparty[df$pair3_11 == "Democrat" & df$Rpid > 3] <- 0
df$judge6_shareparty[df$pair3_11 == "Republican" & df$Rpid > 4] <- 1
df$judge6_shareparty[df$pair3_11 == "Republican" & df$Rpid < 5] <- 0
table(df$judge6_shareparty)

#pair 4

table(df$pair4_4, df$Rpid)
df$judge7_shareparty[df$pair4_4 == "Democrat" & df$Rpid < 4] <- 1
df$judge7_shareparty[df$pair4_4 == "Democrat" & df$Rpid > 3] <- 0
df$judge7_shareparty[df$pair4_4 == "Republican" & df$Rpid > 4] <- 1
df$judge7_shareparty[df$pair4_4 == "Republican" & df$Rpid < 5] <- 0
table(df$judge7_shareparty)

table(df$pair4_11, df$Rpid)
df$judge8_shareparty[df$pair4_11 == "Democrat" & df$Rpid < 4] <- 1
df$judge8_shareparty[df$pair4_11 == "Democrat" & df$Rpid > 3] <- 0
df$judge8_shareparty[df$pair4_11 == "Republican" & df$Rpid > 4] <- 1
df$judge8_shareparty[df$pair4_11 == "Republican" & df$Rpid < 5] <- 0
table(df$judge8_shareparty)


#pair 5

table(df$pair5_4, df$Rpid)
df$judge9_shareparty[df$pair5_4 == "Democrat" & df$Rpid < 4] <- 1
df$judge9_shareparty[df$pair5_4 == "Democrat" & df$Rpid > 3] <- 0
df$judge9_shareparty[df$pair5_4 == "Republican" & df$Rpid > 4] <- 1
df$judge9_shareparty[df$pair5_4 == "Republican" & df$Rpid < 5] <- 0
table(df$judge9_shareparty)

table(df$pair5_11, df$Rpid)
df$judge10_shareparty[df$pair5_11 == "Democrat" & df$Rpid < 4] <- 1
df$judge10_shareparty[df$pair5_11 == "Democrat" & df$Rpid > 3] <- 0
df$judge10_shareparty[df$pair5_11 == "Republican" & df$Rpid > 4] <- 1
df$judge10_shareparty[df$pair5_11 == "Republican" & df$Rpid < 5] <- 0
table(df$judge10_shareparty)



##Code shared race
#pair 1
table(df$pair1_1, df$Rrace)
df$judge1_sharerace <- 0
df$judge1_sharerace[df$pair1_1 == "Asian American" & df$Rrace == "Asian"] <- 1
df$judge1_sharerace[df$pair1_1 == "Black" & df$Rrace == "Black"] <- 1
df$judge1_sharerace[df$pair1_1 == "Hispanic" & df$Rrace == "Latino"] <- 1
df$judge1_sharerace[df$pair1_1 == "Native American" & df$Rrace == "Native"] <- 1
df$judge1_sharerace[df$pair1_1 == "White" & df$Rrace == "White"] <- 1
table(df$judge1_sharerace, df$Rrace)
table(df$judge1_sharerace)

table(df$pair1_8, df$Rrace)
df$judge2_sharerace <- 0
df$judge2_sharerace[df$pair1_8 == "Asian American" & df$Rrace == "Asian"] <- 1
df$judge2_sharerace[df$pair1_8 == "Black" & df$Rrace == "Black"] <- 1
df$judge2_sharerace[df$pair1_8 == "Hispanic" & df$Rrace == "Latino"] <- 1
df$judge2_sharerace[df$pair1_8 == "Native American" & df$Rrace == "Native"] <- 1
df$judge2_sharerace[df$pair1_8 == "White" & df$Rrace == "White"] <- 1
table(df$judge2_sharerace, df$Rrace)
table(df$judge2_sharerace)

#pair 2

table(df$pair2_1, df$Rrace)
df$judge3_sharerace <- 0
df$judge3_sharerace[df$pair2_1 == "Asian American" & df$Rrace == "Asian"] <- 1
df$judge3_sharerace[df$pair2_1 == "Black" & df$Rrace == "Black"] <- 1
df$judge3_sharerace[df$pair2_1 == "Hispanic" & df$Rrace == "Latino"] <- 1
df$judge3_sharerace[df$pair2_1 == "Native American" & df$Rrace == "Native"] <- 1
df$judge3_sharerace[df$pair2_1 == "White" & df$Rrace == "White"] <- 1
table(df$judge3_sharerace, df$Rrace)
table(df$judge3_sharerace)

table(df$pair2_8, df$Rrace)
df$judge4_sharerace <- 0
df$judge4_sharerace[df$pair2_8 == "Asian American" & df$Rrace == "Asian"] <- 1
df$judge4_sharerace[df$pair2_8 == "Black" & df$Rrace == "Black"] <- 1
df$judge4_sharerace[df$pair2_8 == "Hispanic" & df$Rrace == "Latino"] <- 1
df$judge4_sharerace[df$pair2_8 == "Native American" & df$Rrace == "Native"] <- 1
df$judge4_sharerace[df$pair2_8 == "White" & df$Rrace == "White"] <- 1
table(df$judge4_sharerace, df$Rrace)
table(df$judge4_sharerace)


#pair 3

table(df$pair3_1, df$Rrace)
df$judge5_sharerace <- 0
df$judge5_sharerace[df$pair3_1 == "Asian American" & df$Rrace == "Asian"] <- 1
df$judge5_sharerace[df$pair3_1 == "Black" & df$Rrace == "Black"] <- 1
df$judge5_sharerace[df$pair3_1 == "Hispanic" & df$Rrace == "Latino"] <- 1
df$judge5_sharerace[df$pair3_1 == "Native American" & df$Rrace == "Native"] <- 1
df$judge5_sharerace[df$pair3_1 == "White" & df$Rrace == "White"] <- 1
table(df$judge5_sharerace, df$Rrace)
table(df$judge5_sharerace)

table(df$pair3_8, df$Rrace)
df$judge6_sharerace <- 0
df$judge6_sharerace[df$pair3_8 == "Asian American" & df$Rrace == "Asian"] <- 1
df$judge6_sharerace[df$pair3_8 == "Black" & df$Rrace == "Black"] <- 1
df$judge6_sharerace[df$pair3_8 == "Hispanic" & df$Rrace == "Latino"] <- 1
df$judge6_sharerace[df$pair3_8 == "Native American" & df$Rrace == "Native"] <- 1
df$judge6_sharerace[df$pair3_8 == "White" & df$Rrace == "White"] <- 1
table(df$judge6_sharerace, df$Rrace)
table(df$judge6_sharerace)



#pair 4

table(df$pair4_1, df$Rrace)
df$judge7_sharerace <- 0
df$judge7_sharerace[df$pair4_1 == "Asian American" & df$Rrace == "Asian"] <- 1
df$judge7_sharerace[df$pair4_1 == "Black" & df$Rrace == "Black"] <- 1
df$judge7_sharerace[df$pair4_1 == "Hispanic" & df$Rrace == "Latino"] <- 1
df$judge7_sharerace[df$pair4_1 == "Native American" & df$Rrace == "Native"] <- 1
df$judge7_sharerace[df$pair4_1 == "White" & df$Rrace == "White"] <- 1
table(df$judge7_sharerace, df$Rrace)
table(df$judge7_sharerace)

table(df$pair4_8, df$Rrace)
df$judge8_sharerace <- 0
df$judge8_sharerace[df$pair4_8 == "Asian American" & df$Rrace == "Asian"] <- 1
df$judge8_sharerace[df$pair4_8 == "Black" & df$Rrace == "Black"] <- 1
df$judge8_sharerace[df$pair4_8 == "Hispanic" & df$Rrace == "Latino"] <- 1
df$judge8_sharerace[df$pair4_8 == "Native American" & df$Rrace == "Native"] <- 1
df$judge8_sharerace[df$pair4_8 == "White" & df$Rrace == "White"] <- 1
table(df$judge8_sharerace, df$Rrace)
table(df$judge8_sharerace)


#pair 5

table(df$pair5_1, df$Rrace)
df$judge9_sharerace <- 0
df$judge9_sharerace[df$pair5_1 == "Asian American" & df$Rrace == "Asian"] <- 1
df$judge9_sharerace[df$pair5_1 == "Black" & df$Rrace == "Black"] <- 1
df$judge9_sharerace[df$pair5_1 == "Hispanic" & df$Rrace == "Latino"] <- 1
df$judge9_sharerace[df$pair5_1 == "Native American" & df$Rrace == "Native"] <- 1
df$judge9_sharerace[df$pair5_1 == "White" & df$Rrace == "White"] <- 1
table(df$judge9_sharerace, df$Rrace)
table(df$judge9_sharerace)

table(df$pair5_8, df$Rrace)
df$judge10_sharerace <- 0
df$judge10_sharerace[df$pair5_8 == "Asian American" & df$Rrace == "Asian"] <- 1
df$judge10_sharerace[df$pair5_8 == "Black" & df$Rrace == "Black"] <- 1
df$judge10_sharerace[df$pair5_8 == "Hispanic" & df$Rrace == "Latino"] <- 1
df$judge10_sharerace[df$pair5_8 == "Native American" & df$Rrace == "Native"] <- 1
df$judge10_sharerace[df$pair5_8 == "White" & df$Rrace == "White"] <- 1
table(df$judge10_sharerace, df$Rrace)
table(df$judge10_sharerace)




df$id <- seq(1,nrow(df)) #create id variable for each respondent
summary(df$id)



####################################################################################################
## STACK DATA FRAMES
####################################################################################################


##pair 1: judge 1
cols1 <- df[, c("id", grep("pair1", names(df), value=TRUE)[1:7], "Q124", "Q5_1num", "Q81_1", "Q82_1num", "discrimtype", "Rpid", "Rwoman", "RLGB", "RLGB2", "Rincome", "Rrelig", "Rlibcon", "Rrace", "Redu", "Rage", "Rknow", "Rtrans", "judge1_sharegender", "judge1_sharesexid", "judge1_shareparty", "judge1_sharerace")]

##pair 1: judge 2
cols2 <- df[, c("id", grep("pair1", names(df), value=TRUE)[8:14], "Q124", "Q5_2num", "Q81_2", "Q82_2num", "discrimtype", "Rpid", "Rwoman", "RLGB", "RLGB2", "Rincome", "Rrelig", "Rlibcon", "Rrace", "Redu", "Rage", "Rknow", "Rtrans", "judge2_sharegender", "judge2_sharesexid", "judge2_shareparty", "judge2_sharerace")]


##pair 2: judge 3
cols3 <- df[, c("id", grep("pair2", names(df), value=TRUE)[1:7], "Q83", "Q87_1num", "Q88_1", "Q89_1num", "discrimtype", "Rpid", "Rwoman", "RLGB", "RLGB2", "Rincome", "Rrelig", "Rlibcon", "Rrace", "Redu", "Rage", "Rknow", "Rtrans", "judge3_sharegender", "judge3_sharesexid", "judge3_shareparty", "judge3_sharerace")]

##pair 2: judge 4
cols4 <- df[, c("id", grep("pair2", names(df), value=TRUE)[8:14], "Q83", "Q87_2num", "Q88_2", "Q89_2num", "discrimtype", "Rpid", "Rwoman", "RLGB", "RLGB2", "Rincome", "Rrelig", "Rlibcon", "Rrace", "Redu", "Rage", "Rknow", "Rtrans", "judge4_sharegender", "judge4_sharesexid", "judge4_shareparty", "judge4_sharerace")]

##pair 3: judge 5
cols5 <- df[, c("id", grep("pair3", names(df), value=TRUE)[1:7], "Q90",  "Q94_1num", "Q95_1", "Q96_1num", "discrimtype", "Rpid", "Rwoman", "RLGB", "RLGB2", "Rincome", "Rrelig", "Rlibcon", "Rrace", "Redu", "Rage", "Rknow", "Rtrans", "judge5_sharegender", "judge5_sharesexid", "judge5_shareparty", "judge5_sharerace")]

##pair 3: judge 6
cols6 <- df[, c("id", grep("pair3", names(df), value=TRUE)[8:14], "Q90",  "Q94_2num", "Q95_2", "Q96_2num", "discrimtype", "Rpid", "Rwoman", "RLGB", "RLGB2", "Rincome", "Rrelig", "Rlibcon", "Rrace", "Redu", "Rage", "Rknow", "Rtrans", "judge6_sharegender", "judge6_sharesexid", "judge6_shareparty", "judge6_sharerace")]


##pair 4: judge 7
cols7 <- df[, c("id", grep("pair4", names(df), value=TRUE)[1:7], "Q104",  "Q108_1num", "Q109_1", "Q110_1num", "discrimtype", "Rpid", "Rwoman", "RLGB", "RLGB2", "Rincome", "Rrelig", "Rlibcon", "Rrace", "Redu", "Rage", "Rknow", "Rtrans", "judge7_sharegender", "judge7_sharesexid", "judge7_shareparty", "judge7_sharerace")]

##pair 4: judge 8
cols8 <- df[, c("id", grep("pair4", names(df), value=TRUE)[8:14], "Q104",  "Q108_2num", "Q109_2", "Q110_2num", "discrimtype", "Rpid", "Rwoman", "RLGB", "RLGB2", "Rincome", "Rrelig", "Rlibcon", "Rrace", "Redu", "Rage", "Rknow", "Rtrans", "judge8_sharegender", "judge8_sharesexid", "judge8_shareparty", "judge8_sharerace")]


##pair 5: judge 9
cols9 <- df[, c("id", grep("pair5", names(df), value=TRUE)[1:7], "Q111",  "Q115_1num", "Q116_1", "Q117_1num", "discrimtype", "Rpid", "Rwoman", "RLGB", "RLGB2", "Rincome", "Rrelig", "Rlibcon", "Rrace", "Redu", "Rage", "Rknow", "Rtrans", "judge9_sharegender", "judge9_sharesexid", "judge9_shareparty", "judge9_sharerace")]

##pair 5: judge 10
cols10 <- df[, c("id", grep("pair5", names(df), value=TRUE)[8:14], "Q111",  "Q115_2num", "Q116_2", "Q117_2num", "discrimtype", "Rpid", "Rwoman", "RLGB", "RLGB2", "Rincome", "Rrelig", "Rlibcon", "Rrace", "Redu", "Rage", "Rknow", "Rtrans", "judge10_sharegender", "judge10_sharesexid", "judge10_shareparty", "judge10_sharerace")]




## clean preferred judge variable 

cols1$selected <- ifelse(cols1$Q124 == "Judge 1", 1, 0)

cols2$selected <- ifelse(cols2$Q124 == "Judge 2", 1, 0)

cols3$selected <- ifelse(cols3$Q83 == "Judge 3", 1, 0)

cols4$selected <- ifelse(cols4$Q83 == "Judge 4", 1, 0)

cols5$selected <- ifelse(cols5$Q90 == "Judge 5", 1, 0)

cols6$selected <- ifelse(cols6$Q90 == "Judge 6", 1, 0)

cols7$selected <- ifelse(cols7$Q104 == "Judge 7", 1, 0)

cols8$selected <- ifelse(cols8$Q104 == "Judge 8", 1, 0)

cols9$selected <- ifelse(cols9$Q111 == "Judge 9", 1, 0)

cols10$selected <- ifelse(cols10$Q111 == "Judge 10", 1, 0)

#check coding
table(cols1$Q124, cols1$selected)
table(cols2$Q124, cols2$selected)
table(cols3$Q83, cols3$selected)
table(cols4$Q83, cols4$selected)
table(cols5$Q90, cols5$selected)
table(cols6$Q90, cols6$selected)
table(cols7$Q104, cols7$selected)
table(cols8$Q104, cols8$selected)
table(cols9$Q111, cols9$selected)
table(cols10$Q111, cols10$selected)


#########################
## standardize names
#########################

names(cols1) <- c("id", "race","gender","sexuality","party","age","lawschool","occupation","preference", "conservative", "political", "trustcourt", "discrimtype", "Rpid", "Rwoman", "RLGB", "RLGB2", "Rincome", "Rrelig", "Rlibcon", "Rrace", "Redu", "Rage", "Rknow", "Rtrans","GenderIN", "SexualityIN", "PartyIN", "RaceIN", "selected")

names(cols2) <- c("id", "race","gender","sexuality","party","age","lawschool","occupation","preference", "conservative", "political", "trustcourt", "discrimtype", "Rpid", "Rwoman", "RLGB", "RLGB2", "Rincome", "Rrelig", "Rlibcon", "Rrace", "Redu", "Rage", "Rknow", "Rtrans", "GenderIN", "SexualityIN", "PartyIN", "RaceIN", "selected")

names(cols3) <- c("id", "race","gender","sexuality","party","age","lawschool","occupation","preference", "conservative", "political", "trustcourt", "discrimtype", "Rpid", "Rwoman", "RLGB", "RLGB2", "Rincome", "Rrelig", "Rlibcon", "Rrace", "Redu", "Rage", "Rknow", "Rtrans", "GenderIN", "SexualityIN", "PartyIN", "RaceIN", "selected")

names(cols4) <- c("id", "race","gender","sexuality","party","age","lawschool","occupation","preference", "conservative", "political", "trustcourt", "discrimtype", "Rpid", "Rwoman", "RLGB", "RLGB2", "Rincome", "Rrelig", "Rlibcon", "Rrace", "Redu", "Rage", "Rknow", "Rtrans", "GenderIN", "SexualityIN", "PartyIN", "RaceIN", "selected")

names(cols5) <- c("id", "race","gender","sexuality","party","age","lawschool","occupation","preference", "conservative", "political", "trustcourt","discrimtype", "Rpid", "Rwoman", "RLGB", "RLGB2", "Rincome", "Rrelig", "Rlibcon", "Rrace", "Redu", "Rage", "Rknow", "Rtrans", "GenderIN", "SexualityIN", "PartyIN", "RaceIN", "selected")

names(cols6) <- c("id", "race","gender","sexuality","party","age","lawschool","occupation","preference", "conservative", "political", "trustcourt","discrimtype", "Rpid", "Rwoman", "RLGB", "RLGB2", "Rincome", "Rrelig", "Rlibcon", "Rrace", "Redu", "Rage", "Rknow", "Rtrans", "GenderIN", "SexualityIN", "PartyIN", "RaceIN", "selected")

names(cols7) <- c("id", "race","gender","sexuality","party","age","lawschool","occupation","preference", "conservative", "political", "trustcourt","discrimtype", "Rpid", "Rwoman", "RLGB", "RLGB2", "Rincome", "Rrelig", "Rlibcon", "Rrace", "Redu", "Rage", "Rknow", "Rtrans", "GenderIN", "SexualityIN", "PartyIN", "RaceIN", "selected")

names(cols8) <- c("id", "race","gender","sexuality","party","age","lawschool","occupation","preference", "conservative", "political", "trustcourt","discrimtype", "Rpid", "Rwoman", "RLGB", "RLGB2", "Rincome", "Rrelig", "Rlibcon", "Rrace", "Redu", "Rage", "Rknow", "Rtrans", "GenderIN", "SexualityIN", "PartyIN", "RaceIN", "selected")

names(cols9) <- c("id", "race","gender","sexuality","party","age","lawschool","occupation","preference", "conservative", "political", "trustcourt","discrimtype", "Rpid", "Rwoman", "RLGB", "RLGB2", "Rincome", "Rrelig", "Rlibcon", "Rrace", "Redu", "Rage", "Rknow", "Rtrans", "GenderIN", "SexualityIN", "PartyIN", "RaceIN", "selected")

names(cols10) <- c("id", "race","gender","sexuality","party","age","lawschool","occupation","preference", "conservative", "political", "trustcourt","discrimtype", "Rpid", "Rwoman", "RLGB", "RLGB2", "Rincome", "Rrelig", "Rlibcon", "Rrace", "Redu", "Rage", "Rknow", "Rtrans", "GenderIN", "SexualityIN", "PartyIN", "RaceIN", "selected")

stack <- cbind(rbind(cols1,cols2,cols3,cols4,cols5,cols6, cols7, cols8, cols9, cols10))


names(stack)


## Vignette ID - this is the number of the choice set
stack$vignette_id <- rep(1:5,each = nrow(stack)/5) 

## Respondent - Vignette ID
stack$resp_vignette_id <- paste(stack$id, stack$vignette_id,sep = "")
stack$resp_vignette_id <- as.numeric(as.character(stack$resp_vignette_id))



table(stack$age)

stack$agecat[stack$age == "40 years old"] <- "40s"
stack$agecat[stack$age == "41 years old"] <- "40s"
stack$agecat[stack$age == "42 years old"] <- "40s"
stack$agecat[stack$age == "43 years old"] <- "40s"
stack$agecat[stack$age == "44 years old"] <- "40s"
stack$agecat[stack$age == "45 years old"] <- "40s"
stack$agecat[stack$age == "46 years old"] <- "40s"
stack$agecat[stack$age == "47 years old"] <- "40s"
stack$agecat[stack$age == "48 years old"] <- "40s"
stack$agecat[stack$age == "49 years old"] <- "40s"

stack$agecat[stack$age == "50 years old"] <- "50s"
stack$agecat[stack$age == "51 years old"] <- "50s"
stack$agecat[stack$age == "52 years old"] <- "50s"
stack$agecat[stack$age == "53 years old"] <- "50s"
stack$agecat[stack$age == "54 years old"] <- "50s"
stack$agecat[stack$age == "55 years old"] <- "50s"
stack$agecat[stack$age == "56 years old"] <- "50s"
stack$agecat[stack$age == "57 years old"] <- "50s"
stack$agecat[stack$age == "58 years old"] <- "50s"
stack$agecat[stack$age == "59 years old"] <- "50s"

stack$agecat[stack$age == "60 years old"] <- "60s"
stack$agecat[stack$age == "61 years old"] <- "60s"
stack$agecat[stack$age == "62 years old"] <- "60s"
stack$agecat[stack$age == "63 years old"] <- "60s"
stack$agecat[stack$age == "64 years old"] <- "60s"
stack$agecat[stack$age == "65 years old"] <- "60s"
stack$agecat[stack$age == "66 years old"] <- "60s"
stack$agecat[stack$age == "67 years old"] <- "60s"
stack$agecat[stack$age == "68 years old"] <- "60s"
stack$agecat[stack$age == "69 years old"] <- "60s"

stack$agecat[stack$age == "70 years old"] <- "70+"
stack$agecat[stack$age == "71 years old"] <- "70+"
stack$agecat[stack$age == "72 years old"] <- "70+"
stack$agecat[stack$age == "73 years old"] <- "70+"
stack$agecat[stack$age == "74 years old"] <- "70+"
stack$agecat[stack$age == "75 years old"] <- "70+"
stack$agecat[stack$age == "76 years old"] <- "70+"
stack$agecat[stack$age == "77 years old"] <- "70+"
stack$agecat[stack$age == "78 years old"] <- "70+"
stack$agecat[stack$age == "79 years old"] <- "70+"
stack$agecat[stack$age == "80 years old"] <- "70+"
table(stack$age, stack$agecat)




##Recode DVs to range from 0 to 1 for ease of comparison

table(stack$selected)

table(stack$conservative)

stack$conservative_1 <- stack$conservative/6
table(stack$conservative, stack$conservative_1)

table(stack$political)
stack$political_1 <- stack$political/6
table(stack$political, stack$political_1)

table(stack$trustcourt)
stack$trustcourt_1 <- stack$trustcourt/3
table(stack$trustcourt, stack$trustcourt_1)


write.csv(stack, file = "stackedconjointdata.csv")

